<!--
 * @Author: your name
 * @Date: 2020-12-28 17:53:01
 * @LastEditTime: 2020-12-31 17:12:54
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \basicvue\vuehtml\vueindex.html
-->

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="../assets/css/common.css">
    <link rel="stylesheet" href="../assets/css/vue.css">
    <script src="../assets/js/vue.js"></script>
    <style>
        .buttonClass{
            padding: 3px 20px;
            font-size: 25px;
            margin: 20px 0;
            text-align: center;
            outline: none;
            background:#e67e22;
            color: #ffffff;
            border: none;
            cursor: pointer;
            border-radius: 20px;
        }
        .buttonClass:hover{
            background:#e74c3c;
            color: #000000;
        }
        .score{
            font-size: 30px;
            color: #2ecc71;
            font-weight: bold;
        }
    </style>
</head>
<body>
    <div id="app">
        <a href="../index.html" class="retu">BACK</a>
        <h1>加分实例</h1>
        <div v-liuhouse='color'>{{ score }}</div>
        <div>
            <button :class="buttonClass" @click="addScore">+</button>
        </div>
        <div>
            <button :class="buttonClass" onclick="unbind()">解绑</button>
            <button :class='buttonClass' @click="unb">unb</button>
        </div>
        <div>
            写好了这个功能之后,我们现在就自己定义一个全局指令,这里我们使用Vue.directive();<br/>
            可以看到数字已经变成了红色了，说明自定义指令已经起了作用<br/>
            说明:<br/><br/>
            <ul>
                <li>el:指令所绑定的元素,可以用来直接操作dom</li>
                <li>binding:一个对象，包含指令的很多信息</li>
                <li>vnode:vue编译生成的虚拟节点</li>
            </ul>
        </div>
        
    </div>
    <script>

         //解绑
         function unbind(){
            app.$destroy();
        }

        /*
         *自定义指令
        */
        // Vue.directive('liuhouse',function(el,binding,cnode){
        //     console.log(binding);
        //     el.style = 'color:'+binding.value;
        // });


        /*
         *自定义指令的声明周期
        */
       Vue.directive('liuhouse',{
           //被绑定
           bind:function(el,binding,vnode){
               el.style='color:'+ binding.value;
               console.log('1 - bind');
           },
           //绑定到节点
           inserted:function(){
                console.log('2 - inserted');
           },
           //组件更新
           update:function(){
               console.log('3 - update');
           },
           //组件更新完成
           componentUpdated:function(){
               console.log('4 - componentUpdated');
           },
           //解绑
           unbind:function(){
                console.log('5 - unbind');
           }
       });

        var app = new Vue({
            el : '#app',
            data:{
                score : 10,
                buttonClass:'buttonClass',
                color:'red'
            },
            methods: {
                addScore:function(){
                    this.score++;
                },
                unb:function(){
                    app.$destroy();
                }
            }
        });

       
    </script>
</body>
</html>